<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>add_to_cart.js</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="../../../../../../css/reset.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../../../../../css/main.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../../../../../css/github.css" type="text/css" media="screen" />
<script src="../../../../../../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../../../../js/jquery-effect.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../../../../js/main.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../../../../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>
    <div class="banner">
        
        <h1>
            add_to_cart.js
        </h1>
        <ul class="files">
            
            <li>
                lib/assets/javascripts/sale_app/products/add_to_cart.js
                
            </li>
            <li>Last modified: 2017-01-23 15:25:38 +0800</li>
        </ul>
    </div>

    <div id="bodyContent">
        <div id="content">
  
    <div class="description">
      
<p>function isNumber(num){</p>

<pre><code>return !isNaN(parseFloat(num)) &amp;&amp; isFinite(num)
</code></pre>

<p>}</p>

<p>function addOneNumber(num){</p>

<pre><code>return Number(num) + Number(1)
</code></pre>

<p>}</p>

<p>function reduceOneNumber(num){</p>

<pre><code>return Number(num) - Number(1)
</code></pre>

<p>}</p>

<p>jQuery(function($){</p>

<pre><code>// start add to cart
$(&quot;a#add_to_cart_items&quot;).on(&quot;click&quot;, function(e){
              e.preventDefault();
              var quantity = $(&quot;input#add_to_cart_items_quantity_in_show&quot;).val()
              var productId = $(this).data(&quot;id&quot;);
              var storeId = $(this).data(&quot;store-id&quot;);
  if(isNumber(quantity)){
    // is a number
    $.ajax({
                      url: &quot;/api/store_customers/is_logged&quot;,
                      method: &quot;get&quot;,
                      success: function(data){
                              if(data.status == true){
                                      var csrf = $(&quot;input#js-sale-product-show&quot;).val();
                                      $.ajax({
                                              url: &quot;/api/cart_items&quot;,
                                              method: &quot;post&quot;,
                                              headers: {
                      &#39;X-CSRF-Token&#39;: csrf
                    },
                                              data: {store_id: storeId, store_product_id: productId, quantity: quantity},
                                              success: function(data){
                                                      if(data.status == true){
                                                              location.href = &quot;/sales/cart_items/&quot;+ data.cart_item_id +&quot;/successful?store_id=&quot;+ storeId
                                                      }else {
                                                              showErrorView(data.message);
                                                      }
                                              }
                                      })
                              }else {
                                      showErrorView(&quot;请您先进行登录!&quot;);
                                      return false;
                              }
                      }
              })
  }else {
    console.log(quantity);
    console.log(isNumber(quantity));
    showErrorView(&quot;请输入正确的数字!&quot;);
    return false
  }
      });
// end to add cart

// 添加到购物车数量的控制
      $(&quot;a.js-add-to-cart-num&quot;).on(&quot;click&quot;, function(){
              var num = $(&quot;input#add_to_cart_items_quantity_in_show&quot;).val();
              if(isNumber(num)){ // true is number
                      $(&quot;input#add_to_cart_items_quantity_in_show&quot;).val(Number(num) + Number(1));
                      $(&quot;a.js-reduce-to-cart-num&quot;).removeClass(&quot;disabled&quot;);
              }else {
                      $(&quot;input#add_to_cart_items_quantity_in_show&quot;).val(1);
                      $(&quot;a.js-reduce-to-cart-num&quot;).addClass(&quot;disabled&quot;);
              }
      });

      $(&quot;a.js-reduce-to-cart-num&quot;).on(&quot;click&quot;, function(){
              var num = $(&quot;input#add_to_cart_items_quantity_in_show&quot;).val();
              if(isNumber(num)){ // true is number
                      var currentNum = Number(num) - Number(1)
                      if(currentNum &lt; 1){
                              $(&quot;input#add_to_cart_items_quantity_in_show&quot;).val(1);
                              $(&quot;a.js-reduce-to-cart-num&quot;).addClass(&quot;disabled&quot;);
                      }else {
                              $(&quot;input#add_to_cart_items_quantity_in_show&quot;).val(currentNum);
                              $(&quot;a.js-reduce-to-cart-num&quot;).removeClass(&quot;disabled&quot;);
                      }
              }else {
                      // is not a number
                      $(&quot;input#add_to_cart_items_quantity_in_show&quot;).val(1);
                      $(&quot;a.js-reduce-to-cart-num&quot;).addClass(&quot;disabled&quot;);
              }
      });</code></pre>

<p>});</p>

    </div>
  


  


  
  


  


  

  



  

    

    

    


    


    <!-- Methods -->
              </div>

    </div>
  </body>
</html>
